package com.llb666.e.shop.infracore.config;

//import com.llb666.e.shop.infracore.interceptor.UserTokenInterceptor;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 斌~
 * @version 1.0
 * @date 2020/10/3 11:19 上午
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    /**
     * 实现静态资源的映射
     * @param registry 注册对象
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        String os = System.getProperty("os.name");
        if (os.toLowerCase().startsWith("win")) {  //如果是Windows系统
            registry.addResourceHandler("/**")
                    .addResourceLocations("classpath:/META-INF/resources/")   // 映射swagger2
                    .addResourceLocations("file:C:/upload/");   // 映射本地静态资源
        } else {  //linux 和mac
            registry.addResourceHandler("/**")
                    .addResourceLocations("classpath:/META-INF/resources/")//swagger2页面
                    .addResourceLocations("file:/Users/mac/Downloads/upload/");   // 映射本地静态资源
        }
    }

    @Bean
    public RestTemplate restTemplate(RestTemplateBuilder templateBuilder) {
        //先获取到converter列表
        List<HttpMessageConverter<?>> converters = templateBuilder.build().getMessageConverters();
        for(HttpMessageConverter<?> converter : converters){
            //因为我们只想要jsonConverter支持对text/html的解析
            if(converter instanceof MappingJackson2HttpMessageConverter){
                try{
                    //先将原先支持的MediaType列表拷出
                    List<MediaType> mediaTypeList = new ArrayList<>(converter.getSupportedMediaTypes());
                    //加入对text/html的支持
                    mediaTypeList.add(MediaType.TEXT_HTML);
                    //将已经加入了text/html的MediaType支持列表设置为其支持的媒体类型列表
                    ((MappingJackson2HttpMessageConverter) converter).setSupportedMediaTypes(mediaTypeList);
                }catch(Exception e){
                    e.printStackTrace();
                }
            }
        }
        return templateBuilder.build();
    }

//    @Bean
//    public UserTokenInterceptor userTokenInterceptor() {
//        return new UserTokenInterceptor();
//    }

    /**
     * 注册拦截器(登录拦截)
     * @param registry 注册对象
     */
//    @Override
//    public void addInterceptors(InterceptorRegistry registry) {
//        registry.addInterceptor(userTokenInterceptor())
//                .addPathPatterns("/shopcart/add","/shopcart/del")
//                .addPathPatterns("/address/list","/address/add","/address/update","/address/delete","/address/setDefalut")
//                .addPathPatterns("/order/*")
//                .addPathPatterns("/center/*")
//                .addPathPatterns("/userInfo/*")
//                .addPathPatterns("/myorders/*")
//                .addPathPatterns("/mycomments/*")
////                .addPathPatterns("/payment/*")
////                .excludePathPatterns("/passport/logout")
//                .excludePathPatterns("/myorders/deliver")
//                .excludePathPatterns("/orders/notifyMerchantOrderPaid");
//
//        WebMvcConfigurer.super.addInterceptors(registry);
//    }
}
